package com.apps.vip.service.impl;

import com.apps.globals.activity.enums.ActivityType;
import com.apps.globals.activity.model.po.KupaActivity;
import com.apps.globals.activity.service.IKupaActivityService;
import com.apps.sworld.dao.impl.sDaoImpl;
import com.apps.vip.model.po.KupaVipGoods;
import com.apps.vip.dao.KupaVipGoodsMapper;
import com.apps.vip.service.IKupaVipGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大鹏
 * @since 2020-11-04
 */
@Slf4j
@Service
public class KupaVipGoodsServiceImpl extends sDaoImpl<KupaVipGoodsMapper, KupaVipGoods> implements IKupaVipGoodsService
{

    @Autowired
    private IKupaActivityService activityService;

    @Override
    public List<KupaVipGoods> queryByActivity()
    {
        QueryWrapper<KupaVipGoods> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("recommend");
        List<KupaVipGoods> result=query(wrapper);
        for(int i=0;i<result.size();i++)
        {
            KupaActivity activity=activityService.getOne(ActivityType.OPENVIP,result.get(i).getPrice());
            result.get(i).setPrice(null == activity?result.get(i).getPrice():result.get(i).getPrice().subtract(activity.getAmount_price()));
        }
        return result;
    }

    @Override
    public void onLoad()
    {
        log.info("对比VIP商品的价格和原价是否相等");
        List<KupaVipGoods> result=query();
        for(KupaVipGoods vipGoods:result)
        {
            if(vipGoods.getPrice().compareTo(vipGoods.getOri_price()) != 0)
            {
                vipGoods.setOri_price(vipGoods.getPrice());
                updateById(vipGoods);
            }
        }
    }
}
